<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-22 10:26:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-23 12:51:33
 * @Description: 
 * 
-->
<template>
  <el-dialog
    v-model="visible"
    :title="titleMap[mode]"
    :width="700"
    destroy-on-close
    @closed="$emit('closed')"
    draggable
    overflow
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
          <el-row :gutter="40" justify="space-evenly">
            <el-col :span="10">
              <el-form-item label="经手人" prop="employee_id">
                <skAutoChoose
                  v-model="form.employee.name"
                  :name="'user'"
                  :asKey="'employee_id'"
                  :assign="form"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="订单号" prop="order_code">
                <el-input v-model="form.order_code"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="10">
              <el-form-item label="付款日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="付款单号" prop="code">
                <el-input
                  v-model="form.code"
                  readonly
                  placeholder="自动生成"
                  class="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="收款单位" prop="company_id">
                <skAutoChoose
                  v-model="form.company.name"
                  :name="'factory'"
                  :asKey="'company_id'"
                  :assign="form"
                  @select="companySelect"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>

            <el-col :span="10">
              <el-form-item label="收款抬头" prop="billtrade">
                <!-- <el-input v-model="form.billtrade" /> -->
                <skAutoChoose
                  v-model="form.billtrade"
                  :name="'billtrade'"
                  :params="{ company_id: form.company_id }"
                />
              </el-form-item>
            </el-col>

            <el-col :span="10">
              <el-form-item label="付款账号" prop="subject_id">
                <sk-select-group
                  v-model="form.subject_id"
                  :list="accountList"
                  @select="subjectSelect"
                />
              </el-form-item>
            </el-col>

            <el-col :span="10">
              <el-form-item label="付款抬头" prop="trade_id">
                <skDicSelect
                  v-model="form.trade_id"
                  :code="'trade'"
                  :editsource="'default'"
                >
                </skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-row :gutter="0">
                <el-form-item label="付款金额" prop="amount">
                  <el-col :span="10">
                    <el-input v-model="form.amount"></el-input>
                  </el-col>
                  <el-col :span="4" class="text-center">
                    <span>应付</span>
                  </el-col>
                  <el-col :span="10">
                    <el-input
                      v-model="form.company.receivable_amount"
                      disabled
                    ></el-input>
                  </el-col>
                </el-form-item>
              </el-row>
            </el-col>
            <el-col :span="10">
              <el-form-item label="是否含税" prop="tax_id">
                <skDicSelect v-model="form.tax_id" :code="'FinanceTax'"> </skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="21">
              <el-form-item label="摘要" prop="summary">
                <el-input
                  v-model="form.summary"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                  type="textarea"
                  placeholder="默认：收款-往来单位 金额"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload
          v-model="form.attachments"
          :limit="10"
          draggable
          tip="单个文件不要超过10M,请上传图像格式文件"
        >
        </sk-upload>
      </el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skSelectGroup from "@/components/skSelectGroup";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup, skDicSelect, skAutoChoose },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      accountList: [],
      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        employee: {},
        company: {},
        company_id: -1,
        subject: {},
        trade: {},
        code: "",
        rate: 1,
        attachments: [],
        tax_id: 0,
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
        employee_id: { required: true, message: "请选择经手人" },
        company_id: { required: true, message: "请选择往来单位" },
        subject_id: { required: true, message: "请选择付款账户" },
        trade_id: { required: true, message: "请选择交易单位" },
        amount: { required: true, message: "请填写付款金额" },
      },
      visible: false,
      isSaveing: false,
    };
  },

  mounted() {
    this.getAccount();
    let userInfo = this.$TOOL.data.get("USER_INFO");
    this.form.employee.name = userInfo.username;
    this.form.employee_id = userInfo.id;
  },
  methods: {
    subjectSelect(data) {
      this.form.trade_id = data.trade_id == 0 ? 1015 : data.trade_id;
      //盘点是否有票
      if (data.trade_id == 1000) {
        this.form.tax_id = 0;
      } else {
        this.form.tax_id = 1;
      }
    },
    companySelect(data) {
      this.form.company = data;
      if ((data.billtrade ?? []).length > 0) {
        this.form.billtrade = data.billtrade[0].name;
      }
    },
    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.finance.lender.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    getAccount() {
      this.$API.basedata.subject.list
        .get({ filter: [{ parent_id: { value: [1002, 1012], type: "in" } }] })
        .then((res) => {
          this.accountList = res.data;
        });
    },
    //表单注入数据
    setData(data) {
      this.form = this.$TOOL.removeNull(data);
    },
  },
};
</script>
<style></style>
